package ua.kiev.tosik.price.conf;

import org.apache.log4j.Logger;
import org.w3c.dom.*;

import javax.swing.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class ChapterConfig {

	private static ChapterConfig ourInstance;
	private List<Chapter> chapterList = new ArrayList<Chapter>();
	private Connection conn;
	private static Logger logger = Logger.getLogger(ChapterConfig.class);
	private String lang;
	private String message;
	private String xmlConfig = "conf/modules.xml";

	public static ChapterConfig getInstance() {
		if (ourInstance == null) {
			ourInstance = new ChapterConfig();
		}
		return ourInstance;
	}

	private ChapterConfig() {
		logger.debug("Initialize chapter list class");
		lang = AppConfig.getInstance().get("app.language");
		// conn = DbSession.getInstance().getConn();
		updateLocale();
	}

	private void loadChapters() throws Exception {
		chapterList.clear();

		File file = new File(xmlConfig);
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.parse(file);
		doc.getDocumentElement().normalize();
		NodeList nodeLst = doc.getElementsByTagName("module");
		for (int s = 0; s < nodeLst.getLength(); s++) {
			Node fstNode = nodeLst.item(s);
			if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
				NamedNodeMap attrList = fstNode.getAttributes();
				String multiple = attrList.getNamedItem("multiple").getNodeValue();
				NodeList name = ((Element) fstNode).getElementsByTagName("name-" + lang);
				NodeList clazz = ((Element) fstNode).getElementsByTagName("class");
				NodeList icon = ((Element) fstNode).getElementsByTagName("icon");

				Chapter ch = new Chapter();
				ch.setId(s);
				ch.setName(name.item(0).getTextContent().trim());
				ch.setClassName(clazz.item(0).getTextContent().trim());
				ch.setImageSmall( new ImageIcon(icon.item(0).getTextContent().trim()) );
				ch.setMultiple(multiple.equalsIgnoreCase("true"));
				chapterList.add(ch);
			}
		}

		/*
		Chapter ch = new Chapter();
		ch.setId(1);
		ch.setName("Excel Parser");
		ch.setClassName("ua.kiev.tosik.price.modules.excel.ExcelModule");
		ch.setImageSmall( new ImageIcon("images/DatabaseTable16.png"));
		chapterList.add(ch); */
		/*
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery("SELECT ID, NAME_" + lang + ", IMAGE_SMALL, CLASS_NAME FROM APP.CHAPTERS ORDER BY UPPER(NAME)");
		while ( rs.next() ) {
			Chapter ch = new Chapter();
			ch.setId(rs.getInt("id"));
			ch.setName(rs.getString("name_" + lang));
			ch.setClassName(rs.getString("class_name"));
			ch.setImageSmall( new ImageIcon(rs.getString("image_small")) );
			chapterList.add(ch);
		}
		*/
	}

	public List<Chapter> getChapterList() {
		return chapterList;
	}

	public void updateLocale() {
		lang = AppConfig.getInstance().get("app.language");
		try {
			loadChapters();
		} catch (Exception e) {
			logger.fatal(e);
			message = "Can't load module config!";
		}
		logger.debug("Found " + chapterList.size() + " chapters");
	}

	public String getMessage() {
		return message;
	}

}
